﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.Configuration;
using System.Data;
using TGIApp.Model;

namespace TGIApp.Web.Controllers
{
    public class AgentController : Controller
    {
        //
        // GET: /Agent/

        public ActionResult Index()
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var agent = context.AllUsers.Where(user => user is Agent);
            //return View(agent);
            return View();
        }

        //
        // GET: /Agent/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Agent/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Agent/Create

        [HttpPost]
        public ActionResult Create(User agent)
        {

            //if (ModelState.IsValid)
            //{
            //    var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));

            //    //context.MergeOption = System.Data.Services.Client.MergeOption.AppendOnly;
            //    var role = context.Roles.Where(r => r.Name.ToLower() == "agent").FirstOrDefault();
            //    // var address = new Address() { Line1 = "Line1", Line2 = "Line2", PostalCode = "1111", State = "State", City = "City", Country = "Country" };
            //    var address = agent.Address;
            //    context.AddToAddresses(address);


            //    // user.Address = address;
            //    agent.Role = role;

            //    context.AddToAllUsers(agent);

            //    context.SetLink(agent, "Role", role);
            //    context.SetLink(agent, "Address", address);

            //    context.SaveChanges();

            //}

            //return RedirectToAction("Index");
            return View();
        }

        //
        // GET: /Agent/Edit/5

        public ActionResult Edit(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var agent = (Agent)context.AllUsers.Expand("Address").Where(user => user is Agent && user.Id == id).FirstOrDefault();
            //return View(agent);
            return View();
        }

        //
        // POST: /Agent/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, User agent)
        {

            //if (ModelState.IsValid)
            //{
            //    var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //    context.AttachTo("AllUsers", agent);
            //    context.AttachTo("Addresses", agent.Address);
            //    context.UpdateObject(agent.Address);
            //    context.UpdateObject(agent);

            //    context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);

            //}

            //return RedirectToAction("Index");
            return View();
        }

        //
        // GET: /Agent/Delete/5

        public ActionResult Delete(int id)
        {
            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var agent = (Agent)context.AllUsers.Where(user => user is Agent && user.Id == id).FirstOrDefault();
            //return View(agent);
            return View();
        }

        //
        // POST: /Agent/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, User agentToBeDeleted)
        {

            //var context = new TicketsModelContainer(new Uri(ConfigurationManager.AppSettings["DataServiceUri"]));
            //var agent = (Agent)context.AllUsers.Expand("Address").Where(user => user is Agent  && user.Id == id).FirstOrDefault();

            //context.DeleteObject(agent);
            //context.DeleteObject(agent.Address);
            //context.SaveChanges(System.Data.Services.Client.SaveChangesOptions.Batch);
            //return RedirectToAction("Index");

            return View();
        }
    }
}
